package org.yeruyi.common.ui;

import android.app.Dialog;
import android.content.Context;
import android.support.v7.widget.LinearLayoutCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.yeruyi.common.R;


/**
 * Author：yeruyi
 * Date：2017/4/30
 * Version：v1.0
 * Note：
 * 2017年04月30日 新建该控件
 * 2017年10月9日 对透明度进行调整
 */

public class CustomProcessDialog extends Dialog {

    private Builder mBuilder;

    public CustomProcessDialog(Context context) {
        super(context);
    }

    CustomProcessDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    Builder getBuilder() {
        return mBuilder;
    }

    void setBuilder(Builder builder) {
        mBuilder = builder;
    }

    /**
     * Use the msg to judge the dialog is same or not
     * @param msg msg
     * @return boolean
     */
    public boolean isSame(String msg) {
        if (msg == null)
            return false;
        return msg.equals(getBuilder().msg);
    }

    public void setMsg(String msg){
        mBuilder.setMessage(msg);
    }

    public static class Builder {
        private Context mContext;
        private String msg;

        public Builder(Context context) {
            this.mContext = context;
        }

        public Builder setMessage(String message) {
            this.msg = message;
            return this;
        }

        public CustomProcessDialog init() {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final CustomProcessDialog dialog = new CustomProcessDialog(mContext, R.style.custom_process_dialog);
            View layout = inflater.inflate(R.layout.item_custom_process_dialog, null);
            dialog.addContentView(layout, new LinearLayoutCompat.LayoutParams(
                    LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            ((TextView) layout.findViewById(R.id.item_process_msg_tv)).setText(msg == null || msg.length() == 0 ? "请稍后" : msg);
            Window window = dialog.getWindow();
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.alpha = 0.6f;
            window.setAttributes(lp);
            dialog.setContentView(layout);
            dialog.setCancelable(false);
            dialog.setBuilder(this);
            return dialog;
        }
    }
}
